package finalVersion;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class LoginFrame extends JFrame implements ActionListener{

	private JTextField txtServerIP;
	private JTextField txtServerPort;
	private JTextField txtUserId;
	private JButton btnConnect;
	private JLabel lblStatus;

	public LoginFrame() {
		initGUI();
	}

	private void initGUI() {
		JPanel panel = new JPanel(new MigLayout());

		getContentPane().add(panel);

		txtServerIP = new JTextField(20);
		txtServerPort = new JTextField(20);
		txtUserId = new JTextField(20);
		btnConnect = new JButton("Connect");
		btnConnect.setActionCommand("connect");
		btnConnect.addActionListener(this);

		lblStatus = new JLabel("");

		panel.add(new JLabel("Server IP"));
		panel.add(txtServerIP, "wrap");
		panel.add(new JLabel("Server Port"));
		panel.add(txtServerPort, "wrap");
		panel.add(new JLabel("UserID"));
		panel.add(txtUserId, "wrap");
		panel.add(btnConnect, "wrap,center");

		panel.add(lblStatus);
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.pack();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
//	@Override
//	public void mouseClicked(MouseEvent e) {
//		// TODO Auto-generated method stub
//		super.mouseClicked(e);
//	}

	@Override
	public void actionPerformed(ActionEvent event) {
		JButton  component = (JButton) event.getSource();

		if (component.getActionCommand() == "connect") {
			try {
				String serverIP = txtServerIP.getText();
				String serverPort = txtServerPort.getText();
				String userId = txtUserId.getText();

				Socket clientSocket = new Socket(serverIP, Integer
						.parseInt(serverPort));

////				ClientGUI clientGUI = new ClientGUI();
//				clientGUI.setVisible(true);
//				clientGUI.setSocket(clientSocket);
//				
//				new ClientGUI().setVisible(true);
//				
//				this.dispose();

			} catch (Exception ex) {
				lblStatus.setText("Can not connect. Try again.");
			}
		} else {

		}
	}

	
}
